package com.tarena.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

import com.tarena.dao.BookingDao;
import com.tarena.dao.MeettingaddressDao;
import com.tarena.dao.MeettingroomDao;
import com.tarena.entity.Booking;
import com.tarena.entity.Meettingaddress;
import com.tarena.entity.Meettingroom;
import com.tarena.service.BookingService;

/**
 * liubiao
 */
@Service(value="BookingService")
public class BookingServiceImpl implements BookingService {
	
	@Resource
	BookingDao bookingdao;
	
	@Resource
	MeettingaddressDao meetingaddressdao;
	
	@Resource
	MeettingroomDao meetingroomdao;
	
	//增加
	public boolean save(Booking booking) {
		Integer sum = bookingdao.getSum();
		bookingdao.save(booking);
		if (sum < bookingdao.getSum()) {
			return true;
		}
		return false;
	}

	//删除
	public boolean delete(Integer id) {
		Integer sum = bookingdao.getSum();
		bookingdao.delete(id);
		if (sum > bookingdao.getSum()) {
			return true;
		}
		return false;
	}

	//修改
	public void update(Booking booking) {
		bookingdao.update(booking);
	}

	//通过id查询
	public List<Booking> findById(Integer id) {
		List<Booking> list = bookingdao.findById(id);
		return list;
	}
	
	//通过userId查询
	public List<Booking> findByUserId(Integer id) {
		return bookingdao.findByUserId(id);
	}

	//查询所有
	public List<Booking> findAllBooking() {
		List<Booking> list = bookingdao.findAllBooking();
		return list;
	}

	//分页2
	public List<Booking> findByPage(int offset, int limit, Booking booking)
			throws Exception {
		RowBounds rb = new RowBounds(offset, limit);
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		SqlSessionFactory sf = (SqlSessionFactory) ctx.getBean(SqlSessionFactory.class);
		SqlSession session = sf.openSession();
		List<Booking> list = new ArrayList<Booking>();
		
		
		if (null!=booking.getUserId()&&null== booking.getMeettingRoomId()){
			list = session.selectList("findBookingByUserId", booking, rb);
			session.close();
		} else if (null==booking.getUserId()&&null!= booking.getMeettingRoomId()){
			list = session.selectList("findBookingByMeettingRoomId", booking, rb);
			session.close();
		} else {
			list = session.selectList("findBookingByParam", booking, rb);
			session.close();
		}		
		session.close();
		return list;
	}

	//分页1
	public List<Booking> findByPage(int offset, int limit) throws Exception {
		RowBounds rb = new RowBounds(offset, limit);
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		SqlSessionFactory sf = (SqlSessionFactory) ctx
				.getBean(SqlSessionFactory.class);
		SqlSession session = sf.openSession();
		List<Booking> list = session.selectList("findAllBooking", null, rb);
		session.close();
		return list;
	}

	//获取所有记录
	public Integer getSum() {
		return bookingdao.getSum();
	}

	//通过会议室找会议地址
	public List<Meettingroom> getMeetingroomByMeettingaddress(
			Meettingaddress meettingaddress) {
		Meettingaddress ma = meetingaddressdao.findMeettingAddressAndMeettingRoom(meettingaddress.getId());
		return ma.getMeettingrooms();
	}
	
	//通过预定找到会议室
	public List<Booking> getBookingByMeettingroom(
			Meettingroom meettingroom) {
		return meetingroomdao.findMeettingroomAndBooking(meettingroom.getId()).getBookings();
	}
	
	//通过预定找会议地址
	public List<Booking> getBookingByMeettingaddress(
			Meettingaddress meettingaddress) {
		 List<Booking> returns = new ArrayList<Booking>();
		List<Meettingroom> meetingroomByMeettingaddress = getMeetingroomByMeettingaddress(meettingaddress);
		for(Meettingroom meettingroom : meetingroomByMeettingaddress)
		{
			List<Booking> bookingByMeettingroom = getBookingByMeettingroom(meettingroom);
			for(Booking booking : bookingByMeettingroom){
				returns.add(booking);
			}
		}
		
		return returns;
	}

	//时间比较
	public boolean compartwotime(Booking booking, String startime,
			String endtime) {
		String bookingstartime = booking.getStartDate();
		String bookingendtime = booking.getEndDate();
		if(endtime.compareTo(startime)>0){
			if((startime.compareTo(bookingendtime)>0||bookingstartime.compareTo(endtime)>0)){
				return true;
			}
		}
		return false;
		
	}

	//
	public Meettingroom giveMeettingroom(Meettingaddress meettingaddress,
			String startime, String endtime) {
		boolean tsult = true;
		//获取所有的会议室
		List<Meettingroom> meetingroomByMeettingaddress = getMeetingroomByMeettingaddress(meettingaddress);
		for(Meettingroom meettingroom : meetingroomByMeettingaddress){
			//获取一个会议室的所有的预定记录
			List<Booking> bookingByMeettingroom = getBookingByMeettingroom(meettingroom);
			tsult = true;
			for(Booking booking : bookingByMeettingroom){
				if(compartwotime(booking,startime,endtime ) == false){
					tsult = false;
					break;	
				}
					
			}
			if(tsult == true){
				return meettingroom;
			}
		}
		return null;
	}

	//
	public List<Meettingroom> giveMeettingroomAll(Meettingaddress meettingaddress,
			String startime, String endtime) {
		boolean tsult = true;
		List<Meettingroom>  list= new ArrayList<Meettingroom>();
		//获取所有的会议室
		List<Meettingroom> meetingroomByMeettingaddress = getMeetingroomByMeettingaddress(meettingaddress);
		for(Meettingroom meettingroom : meetingroomByMeettingaddress){
			//获取一个会议室的所有的预定记录
			List<Booking> bookingByMeettingroom = getBookingByMeettingroom(meettingroom);
			tsult = true;
			for(Booking booking : bookingByMeettingroom){
				if(compartwotime(booking,startime,endtime ) == false){
					tsult = false;
					break;	
				}
					
			}
			if(tsult == true){
				list.add(meettingroom);
			}
		}
		return list;
	}

	
	
}
